package com.jt9003.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.jt9003.pojo.StockInquiry;
import com.jt9003.pojo.WarehouseExcel;
import com.jt9003.service.StockInquiryService;
import com.jt9003.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/StockInquiryController")
public class StockInquiryController {
    @Resource
    private StockInquiryService stockInquiryService;

    //药品库存查询
    @RequestMapping("/queryList")
    @ResponseBody
    public Object queryStock (@RequestParam(value="drugsName",required=false)  String drugsName,
                              @RequestParam(value ="page",required = false,defaultValue = "1") Integer page,
                              @RequestParam(value ="limit",required = false,defaultValue = "5") Integer limit){
        Map<String,Object> map =new HashMap();
        Map<String,Object> param =new HashMap();
        Integer start=PageUtils.getStart(page,limit);
        param.put("drugsName",drugsName);
        param.put("start",start);
        param.put("limit",limit);
       try {
            List<StockInquiry> list=stockInquiryService.getStockInquiryList(param);
            Integer count=stockInquiryService.getStockInquiryCount(param);
            System.out.print("返回的数据长度"+list.size()+"=====总条数"+count+"=============================");
                map.put( "count",count);
                map.put("code", 0);
                map.put("data",list);
                map.put("success",true);
                map.put("msg","查询成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "发生异常");
        }
        return map;
    }



    //药房药品库存查询
    @RequestMapping("/queryList2")
    @ResponseBody
    public Object queryStock2 (@RequestParam(value="drugsName",required=false)  String drugsName,
                               @RequestParam(value ="page",required = false,defaultValue = "1") Integer page,
                               @RequestParam(value ="limit",required = false,defaultValue = "5") Integer limit){
        Map<String,Object> map =new HashMap();
        Map<String,Object> param =new HashMap();
        Integer start=PageUtils.getStart(page,limit);
        param.put("drugsName",drugsName);
        param.put("start",start);
        param.put("limit",limit);
        try {
            List<StockInquiry> list=stockInquiryService.getStockInquiryList2(param);
            Integer count=stockInquiryService.getStockInquiryCount2(param);
            map.put("code", 0);
            map.put("data",list);
            map.put("count",count);
            map.put("success",true);
            map.put("msg","查询成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "发生异常");
        }
        return map;
    }




    //以下测试
    //查询库存表是否存在此药品
    @RequestMapping("/isDrugs")
    @ResponseBody
    public Object isDrugs (@RequestParam(value="drugsName",required=false)  String drugsName,
                           @RequestParam(value ="drType",required = false) String drType,
                           @RequestParam(value ="inventoryNumber",required = false) Integer inventoryNumber,
                           @RequestParam(value ="warehouseUnit",required = false) String warehouseUnit){
        Map<String,Object> map =new HashMap();
        Map<String,Object> param =new HashMap();
        param.put("drugsName",drugsName);
        param.put("drType",drType);
        param.put("inventoryNumber",inventoryNumber);
        param.put("warehouseUnit",warehouseUnit);
        try {
            StockInquiry stockInquiry=stockInquiryService.isDrugs(param);
            if(stockInquiry!=null) {
                System.out.print("库存药品名：" + stockInquiry.getDrugsName() + "==============================");
                map.put("data", stockInquiry);
                map.put("success", true);
                map.put("msg", "查询成功");
            }else{
                map.put("data", null);
                map.put("success", false);
                map.put("msg", "药品不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "发生异常");
        }
        return map;
    }


    @RequestMapping("/updateInventory")
    @ResponseBody
    public Object updateInventory (@RequestParam(value="drugsName",required=false)  String drugsName,
                                   @RequestParam(value ="drType",required = false) String drType,
                                   @RequestParam(value ="inventoryNumber",required = false) Integer inventoryNumber,
                                   @RequestParam(value ="warehouseUnit",required = false) String warehouseUnit){
        Map<String,Object> map =new HashMap<String, Object>();
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("drugsName",drugsName);
        param.put("drType",drType);
        param.put("inventoryNumber",inventoryNumber);
        param.put("warehouseUnit",warehouseUnit);
        try {
           Integer num =stockInquiryService.updateInventory(param);
           if(num>0){
               map.put("success",true);
               map.put("msg","修改成功");
           }else{
               map.put("success",true);
               map.put("msg","修改");
           }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "发生异常");
        }
        return map;
    }




    @RequestMapping("/addInventory")
    @ResponseBody
    public Object addInventory (@RequestParam(value="drugsName",required=false)  String drugsName,
                                @RequestParam(value ="drType",required = false) String drType,
                                @RequestParam(value ="inventoryNumber",required = false) Integer inventoryNumber,
                                @RequestParam(value ="warehouseUnit",required = false) String warehouseUnit){
        Map<String,Object> map =new HashMap();
        Map<String,Object> param =new HashMap();
        param.put("drugsName",drugsName);
        param.put("drType",drType);
        param.put("inventoryNumber",inventoryNumber);
        param.put("warehouseUnit",warehouseUnit);
        try {
            Integer num=stockInquiryService.addInventory(param);
            if(num>0){
                map.put("success",true);
                map.put("msg","插入成功");
            }else{
                map.put("success",true);
                map.put("msg","插入失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", "发生异常");
        }
        return map;
    }



    @RequestMapping("/write")
    @ResponseBody
    public void WarehouseExcel(HttpServletRequest request, HttpServletResponse response) {

        OutputStream osOut =null;
        ExcelWriter writer =null;
        try {
            List<StockInquiry> list= stockInquiryService.excelWrite();
            System.out.print(list);
            if (list != null) {
                response.reset();
                response.setContentType("application/vnd.ms-excel;charset=utf-8"); // 表明是excel类型
                String fileName ="stock" + System.currentTimeMillis() + ".xlsx";
                response.setHeader("Content-Disposition", "attachment;filename="+fileName);
                Sheet sheet = new Sheet(1, 0, StockInquiry.class);
                sheet.setAutoWidth(Boolean.TRUE);//设置自适应宽度
                sheet.setSheetName("库存表");
                //如果需要保存到ftp服务器--
                osOut = response.getOutputStream();
                writer = EasyExcelFactory.getWriter(osOut, ExcelTypeEnum.XLSX, true);
                writer.write(list, sheet);
                osOut.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("excel文件导出失败, 失败原因：{}", e);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("excel文件导出失败, 失败原因：{}", e);
        } finally {
            try {
                if(writer != null){
                    writer.finish();
                }
                if (osOut!=null){
                    osOut.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                log.error("excel文件导出失败, 失败原因：", e);
            }
        }










    }
}



